home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4038 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  48 lines

  1. Path: clri6f.gsi.de!kraemer
  2. From: kraemer@clri6f.gsi.de (Michael Kraemer)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Random Number Generation
  5. Date: 16 Feb 1996 16:32:09 GMT
  6. Organization: Technische Hochschule Darmstadt
  7. Message-ID: <4g2bi9$1fls@rs18.hrz.th-darmstadt.de>
  8. References: <199602071623.QAA00075@sable.ox.ac.uk> <Pine.OSF.3.91.960207175121.20357B-100000@hai.hiMolde.no>  <19960207.41F660.11A72@aj050.du.pipex.com> <1100.6613T1273T666@himolde.no> <692.6619T1254T1752@in.net>
  9. NNTP-Posting-Host: clri6f.gsi.de
  10. To: mave@in.net
  11.  
  12. In article <692.6619T1254T1752@in.net>, mave@in.net (John J. Maver, Jr.) writes:
  13. >         I want to generate a randum number, like BASIC allowed you to do, in
  14. > C.  I want a function that I can call with the High and Low numbers and then
  15. > have it return a number between them.
  16. > What I have so far is:
  17. > <sb>
  18. > float rndnum(float low, float high)
  19. > {
  20. >     float num;     /*used to temporarily hold a rnd num*/
  21. >         srand=(time(NULL));     /*generate a new seed*/
  22. >         num=rand()%1000;        /*Try to modify rand()s output to be*/
  23. >         num/=100;               /*between 0 and 1*/
  24. >         num=(num*high)+low;     /*Make num fit the specified range*/
  25. >         return num;
  26. > }
  27. >         It doesn't quite work.  Any hints?
  28. > <sb>
  29.  
  30. Why not sth like that:
  31.  
  32. #include <stdlib.h>
  33.  
  34. ...
  35.  
  36.    num = ( (float) rand() / 32767E0 ) * ( high - low ) + low; 
  37.